package com.carbon.carbonmanager.controller;

import com.carbon.carbonmanager.model.UserInfo;
import com.carbon.carbonmanager.model.UserRegCodeInfo;
import com.carbon.carbonmanager.service.EmailService;
import com.carbon.carbonmanager.service.UserService;
import com.carbon.carbonmanager.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 首页控制器
 */
@RestController
@CrossOrigin
public class HomeController {
    @Autowired
    EmailService emailService;

    @Autowired
    UserService userService;
    /**
     * 请求方法
     */
    @RequestMapping(value = "/getTest", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getTest(){
        return new Response(200,"消息",null);
    }

    /**
     * 将用户注册邮箱和生成的注册码添加到数据库中
     * @param1 注册邮箱
     * */
    @ResponseBody
    @RequestMapping(value = "/sendRegCode", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response sendRegCode(@RequestBody UserRegCodeInfo userRegCodeInfo){
        if(userService.validateEmailIsExisted(userRegCodeInfo)){
            //存在
            return new Response(203,"注册邮箱已被使用",null);
        }else{
            return emailService.sendRegCode(userRegCodeInfo);
        }
    }

    /**
     * 用户注册方法
     * @param1 用户名
     * @param2 注册邮箱
     * @param3 用户密码
     * @param4 注册验证码
     * */
    @ResponseBody
    @RequestMapping(value = "/regUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response regUser(@RequestBody UserInfo userInfo){
        return userService.RegUser(userInfo);
    }

    /**
     * 用户登录功能API
     * @param1 用户名
     * @param2 用户密码
     * */
    @ResponseBody
    @RequestMapping(value = "/validateUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response validateUser(@RequestBody UserInfo userInfo){
        return userService.validateUser(userInfo);
    }
}
